#include "mvc/controller/parameters_controller.h"

Parameters_controller::Parameters_controller()
{
}

/**
 * @brief Parameters_controller::split_parameter
 * @param parameter
 * @param output
 */
void Parameters_controller::split_parameter(QString parameter, QVector<float> &output)
{
    QStringList list = parameter.split('-');
    float first_number_range = list[0].toDouble();
    QString rigth_side = list[1];
    list = rigth_side.split(',');
    float second_number_range = list [0].toDouble();
    float split_number_range =  list [1].toDouble();

    output.push_back(first_number_range);
    output.push_back(second_number_range);
    output.push_back(split_number_range);
}

/**
 * @brief Parameters_controller::intermediate_configuration
 * @param parameter
 * @return
 */
QVector<float> Parameters_controller::intermediate_configuration(QString parameter)
{
    QVector<float> splits;
    split_parameter(parameter, splits);

    QVector<float> intermediate_numbers;

    float intermediate = splits.at(0);
    while (intermediate <= splits.at(1))
    {
        intermediate_numbers.append(intermediate);
        intermediate += splits.at(2);
    }

    if(intermediate > splits.at(1) && !intermediate_numbers.contains(splits.at(1)))
        intermediate_numbers.append(splits.at(1));

    return intermediate_numbers;
}

/**
 * @brief Parameters_controller::cartesian_product
 * @param lists
 * @return
 */
QVector<QVector<float> > Parameters_controller::cartesian_product(QVector<QVector<float> > lists)
{
    QVector<QVector<float> > resultLists(0);
    if (lists.size() == 0) {
        QVector<float>  aux(0);
        resultLists.append(aux);
        return resultLists;
    } else {
        QVector<float> firstList = lists.at(0);
        lists.remove(0);
        QVector<QVector<float> > remainingLists = cartesian_product(lists);
        for(int i = 0 ; i < firstList.size(); ++i){
            for(int j = 0; j < remainingLists.size(); ++j){
                QVector<float>  resultList(0);
                resultList.append(firstList.at(i));
                resultList.operator +=(remainingLists.at(j));
                resultLists.append(resultList);
            }
        }
    }
    return resultLists;
}

/**
 * @brief Parameters_controller::get_configurations
 * @param wp
 * @return
 */
QVector<QVector<float> > Parameters_controller::get_configurations(ParametersWidget *wp,
                                                                   QVector<QPair<QString, QString> *> *intervals)
{
    QVector<QVector<float> > vector_configurations(0);

    QMapIterator<QString, QMap<QString, QString>  > isection(*wp);

    while (isection.hasNext())
    {
        isection.next();

        if(isection.key().compare("airfoils") != 0)
        {
            QMapIterator<QString, QString> iparameter = isection.value();

            while(iparameter.hasNext())
            {
                iparameter.next();

                if(iparameter.value().contains('-'))
                {
                    vector_configurations.append(intermediate_configuration(iparameter.value()));
                    intervals->push_back(new QPair<QString, QString>(isection.key(), iparameter.key()));
                }
                else
                {
                    QVector<float> aux(0);
                    aux.append(iparameter.value().toFloat());
                    vector_configurations.append(aux);
                }
            }
        }
    }

    QVector<QVector<float> > result = cartesian_product(vector_configurations);

    return result;
}

/**
 * @brief Parameters_controller::make_parameters_uavs
 * @param wp
 * @param params_widget_uavs
 * @return
 */
QVector<Parameters*> Parameters_controller::make_parameters_uavs(ParametersWidget *wp,
                                                                 QVector<QVector<float> > params_widget_uavs)
{
    Parameters *p;
    QVector<Parameters*> paramVector(0);

    for (int i = 0; i < params_widget_uavs.size(); ++i)
    {
        QMapIterator<QString, QMap<QString, QString>  > isection(*wp);
        QVector<float> intermediate = params_widget_uavs.at(i);

        p = new Parameters();

        int j = 0;
        while (isection.hasNext())
        {
            isection.next();

            if(isection.key().compare("airfoils") != 0)
            {
                QMapIterator<QString, QString> iparameter = isection.value();
                while(iparameter.hasNext())
                {
                    iparameter.next();
                    ((*p)[isection.key()])[iparameter.key()] = intermediate[j];
                    ++j;
                }

            }
        }

        paramVector.append(p);
    }

    return paramVector;
}
